#ifndef _COLLISION_BOX_MANAGER_H_
#define _COLLISION_BOX_MANAGER_H_
#include "scene.h"
#include "camera.h"
#include "scene_manager.h"
#include <cmath>
#include <vector>

extern SceneManager scene_manager;

extern std::vector<Collision_box> collision_box_list_01;
extern std::vector<Collision_box> collision_box_list_02;
extern std::vector<Collision_box> collision_box_list_03;
extern std::vector<Collision_box> collision_box_list_04;
extern std::vector<Collision_box> collision_box_list_05;
extern std::vector<Collision_box> collision_box_list_06;





class Collision_box_manager
{
public:
	Collision_box_manager() = default;
	~Collision_box_manager() = default;

	/*bool is_collide_x(Collision_box col,std::vector<Collision_box> vec)
	{
		for (Collision_box& collision_box : vec)
		{
			int px1 = col.get_start().x;
			int sx1 = collision_box.get_start().x;
			int px2 = col.get_end().x;
			int sx2 = collision_box.get_end().x;
			if ((abs(px1 - sx2) - (px2 - px1) - (sx2 - sx1) <= 0) && (abs(px2 - sx1) - (px2 - px1) - (sx2 - sx1) <= 0))
			{
				return true;
			}

			return false;

		}
	}
	bool is_collide_y(Collision_box col, std::vector<Collision_box> vec)
	{
		for (Collision_box& collision_box : vec)
		{
			int py1 = col.get_start().y;
			int sy1 = collision_box.get_start().y;
			int py2 = col.get_end().y;
			int sy2 = collision_box.get_end().y;
			if ((abs(py1 - sy2) - (py2 - py1) - (sy2 - sy1) <= 0) && (abs(py2 - sy1) - (py2 - py1) - (sy2 - sy1) <= 0))
			{
				return true;
			}

			return false;

		}
	}*/

	/*void is_collide(Collision_box col, std::vector<Collision_box> vec)
	{
		for (Collision_box& collision_box : vec)
		{
			int px1 = col.get_start().x;
			int sx1 = collision_box.get_start().x;
			int px2 = col.get_end().x;
			int sx2 = collision_box.get_end().x;
			int py1 = col.get_start().y;
			int sy1 = collision_box.get_start().y;
			int py2 = col.get_end().y;
			int sy2 = collision_box.get_end().y;
			int dis_x_1 = abs(px1 - sx2) - (px2 - px1) - (sx2 - sx1);
			int dis_x_2 = abs(px2 - sx1) - (px2 - px1) - (sx2 - sx1);
			int dis_y_1 = abs(py1 - sy2) - (py2 - py1) - (sy2 - sy1);
			int dis_y_2 = abs(py2 - sy1) - (py2 - py1) - (sy2 - sy1);
			if (
				(dis_x_1 <= 0 || dis_x_2 <= 0) && (((dis_x_1 < dis_y_1) && (dis_x_1 < dis_y_2)) || ((dis_x_2 < dis_y_1) && (dis_x_2 < dis_y_2)))
				)
			{
				is_collide_x = true;
			}
			else
				is_collide_x = false;


			if (
				(dis_y_1 <= 0 || dis_y_2 <= 0) && (((dis_y_1 < dis_x_1) && (dis_y_1 < dis_x_2)) || ((dis_y_2 < dis_x_1) && (dis_y_2 < dis_x_2)))
				)
			{
				is_collide_y = true;
			}
			else
				is_collide_y = false;
		}
	}*/

	std::vector<bool> is_collide(Collision_box col, std::vector<Collision_box> vec)
	{
		bool flag_b = true;
		bool flag_t = true;
		bool flag_l = true;
		bool flag_r = true;
		bool flag_s = true;
		bool is_collide_l = false;
		bool is_collide_r = false;
		bool is_collide_t = false;
		bool is_collide_b = false;
		bool is_stuck= false;
		std::vector<bool> ans = {};
		for (Collision_box& collision_box : vec) 
		{
			int px1 = col.get_start().x;
			int sx1 = collision_box.get_start().x;
			int px2 = col.get_end().x;
			int sx2 = collision_box.get_end().x;
			int py1 = col.get_start().y;
			int sy1 = collision_box.get_start().y;
			int py2 = col.get_end().y;
			int sy2 = collision_box.get_end().y;
			if ((py1 < sy1+2) &&  (sy1 < py2 + 2) && (py2 < sy2 + 2)&& !(px2<sx1+10 || px1>sx2-10))
			{
				flag_b= false;
			}
			if ((sy1<py1+2) && (py1 < sy2 + 2) && (sy2 - 2 < py2) && !(px2<sx1 + 10 || px1>sx2 - 10))
			{
				flag_t = false;
			}
			if ((sx1<px1+2)&&(px1 < sx2 + 2) && (sx2 - 2 < px2)&&(sx2-px1<10) && !(py2<sy1 + 10 || py1>sy2 - 10))
			{
				flag_l = false;
			}
			if((px1 < sx1 + 2) && (sx1 - 2 < px2)&&(px2<sx2+2) &&(px2-sx1<10) && !(py2<sy1 + 10 || py1>sy2 - 10))
			{
				flag_r = false;
			}
			if ((sx1 < px1 + 10) && (sx2 > px2 - 10) && (sy1 < py1 + 10) && (py2 < sy2 + 10)) {
				flag_s = false;
			}
		}
		if (!flag_b) is_collide_b = true;
		else is_collide_b = false;
		if (!flag_t) is_collide_t = true;
		else is_collide_t = false;
		if (!flag_l) is_collide_l = true;
		else is_collide_l = false;
		if (!flag_r) is_collide_r = true;
		else is_collide_r = false;
		if (!flag_s) is_stuck = true;
		else is_stuck = false;
		ans.push_back(is_collide_l);
		ans.push_back(is_collide_r);
		ans.push_back(is_collide_t);
		ans.push_back(is_collide_b);
		ans.push_back(is_stuck);
		return ans;
		
	}


	void on_draw(const Camera& camera)
	{
		Scene* scene = scene_manager.get_current_scene();
		if (scene == game_scene)
		{
			for (Collision_box& collision_box : collision_box_list_01)
			{
				collision_box.on_draw(camera);
			}
		}
		else  if (scene == game_scene_02)
		{
			for (Collision_box& collision_box : collision_box_list_02)
			{
				collision_box.on_draw(camera);
			}
		}
		else  if (scene == game_scene_03)
		{
			for (Collision_box& collision_box : collision_box_list_03)
			{
				collision_box.on_draw(camera);
			}
		}
		else  if (scene == game_scene_04)
		{
			for (Collision_box& collision_box : collision_box_list_04)
			{
				collision_box.on_draw(camera);
			}
		}
		else  if (scene == game_scene_05)
		{
			for (Collision_box& collision_box : collision_box_list_05)
			{
				collision_box.on_draw(camera);
			}
		}
		else  if (scene == game_scene_06)
		{
			for (Collision_box& collision_box : collision_box_list_06)
			{
				collision_box.on_draw(camera);
			}
		}
		else;
	}

	void on_update(int delta)
	{

	}

private:
};





#endif // !_COLLISION_BOX_MANAGER_H_

